#pragma warning(disable:4996)
#pragma warning(disable:6031)

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

//int main()
//{
//	char* a[] = { "aaa","bbb","ccc" };
//	char** p = a;
//	p++;
//	printf("%s\n", *p);
//	return 0;
//}

typedef struct SlistNode
{
	int data;
	struct SlistNode* next;
}SlistNode;

SlistNode* BuyNewNode(int data)
{
	SlistNode* newnode = (SlistNode*)malloc(sizeof(SlistNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->data = data;
	newnode->next = NULL;
	return newnode;
}

void Print(SlistNode* phead)
{
	SlistNode* ptr = phead;
	while (ptr != NULL)
	{
		printf("%d ", ptr->data);
		ptr = ptr->next;
	}
}

void PushLast(SlistNode** pphead, int data)
{
	SlistNode* newnode = BuyNewNode(data);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SlistNode* ptr = *pphead;
		while (ptr->next != NULL)
		{
			ptr = ptr->next;
		}
		ptr->next = newnode;
	}
}

void PushFrant(SlistNode** pphead, int data)
{
	SlistNode* newnode = BuyNewNode(data);
	newnode->next = *pphead;
	*pphead = newnode;
}

void PopLast(SlistNode** pphead)
{
	assert(*pphead);
	SlistNode* ptr = *pphead;
	if (ptr->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		while (ptr->next->next != NULL)
		{
			ptr = ptr->next;
		}
		free(ptr->next);
		ptr->next = NULL;
	}
}

void PopFrant(SlistNode** pphead)
{
	assert(*pphead);
	SlistNode* ptr = (*pphead)->next;
	free(*pphead);
	*pphead = ptr;
}

SlistNode* Find(SlistNode* phead, int data)
{
	SlistNode* ptr = phead;
	while (ptr != NULL)
	{
		if (data == ptr->data)
		{
			return ptr;
		}
	}
	return NULL;
}

void Erase(SlistNode** pphead, int data)
{
	assert(*pphead);
	SlistNode* pos = Find(*pphead, data);
	if (*pphead == pos)
	{
		*pphead = pos->next;
		free(pos);
	}
	else
	{
		SlistNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
	pos = NULL;
}

void InsertB(SlistNode** pphead, int f, int data)
{
	assert(*pphead);
	SlistNode* pos = Find(*pphead, f);
	SlistNode* newnode = BuyNewNode(data);
	if (pos == *pphead)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		SlistNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		newnode->next = pos;
		prev->next = newnode;
	}
}

void InsertA(SlistNode* phead, int f, int data)
{
	SlistNode* pos = Find(phead, f);
	SlistNode* newnode = BuyNewNode(data);
	newnode->next = pos->next;
	pos->next = newnode;
}

void destroy(SlistNode** pphead)
{
	assert(*pphead);
	SlistNode* ptr = *pphead;
	while (ptr != NULL)
	{
		SlistNode* next = ptr->next;
		free(ptr);
		ptr = next;
	}
	*pphead = NULL;
}

int main()
{
	SlistNode* p = NULL;
	PushFrant(&p, 1);
	InsertA(p,1, 2);
	InsertB(&p, 1, 3);
	destroy(&p);
	Print(p);
	return 0;
}